# Copyright (C) 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("//tools/build:rules.bzl", "android_dynamic_library", "apic_encoders", "apic_template", "cc_copts", "cc_dynamic_library")

apic_template(
    name = "vulkan_templated",
    api = "//gapis/api/vulkan:api",
    templates = [
        "//gapis/api/templates:api_imports.h",
        "//gapis/api/templates:api_spy.h",
        "//gapis/api/templates:api_spy.cpp",
        "//gapis/api/templates:api_types.h",
        "//gapis/api/templates:api_types.cpp",
        "//gapis/api/vulkan/templates:api_exports.cpp",
        "//gapis/api/vulkan/templates:api_imports.cpp",
        "//gapis/api/vulkan/templates:vk_spy_helpers.cpp",
    ],
)

apic_encoders(
    name = "vulkan_encoders",
    apis = [
        "//gapis/api/vulkan:api",
    ],
    namespace = "gapii",
)

cc_library(
    name = "cc",
    srcs = glob(
        [
            "*.cpp",
            "*.h",
            "*.inc",
        ],
        exclude = [
            "*_test.cpp",
        ],
    ) + [
        ":vulkan_encoders",
        ":vulkan_templated",
    ],
    copts = cc_copts() + select({
        "//tools/build:windows": ["-Wa,-mbig-obj"],
        "//tools/build:fuchsia-arm64": [],
        "//tools/build:fuchsia-x86_64": [],
        "//conditions:default": [],
    }),
    linkopts = select({
        "//tools/build:linux": [],
        "//tools/build:darwin": [
            "-framework Cocoa",
            "-framework OpenGL",
        ],
        "//tools/build:darwin_arm64": [
            "-framework Cocoa",
            "-framework OpenGL",
        ],
        "//tools/build:windows": [],
        "//tools/build:fuchsia-arm64": [
            "-static-libstdc++",
            "-static-libgcc",
        ],
        "//tools/build:fuchsia-x86_64": [
            "-static-libstdc++",
            "-static-libgcc",
        ],
        # Android
        "//conditions:default": [
            "-ldl",
            "-llog",
            "-lm",
            "-Wl,--no-undefined",
        ],
    }),
    visibility = ["//visibility:public"],
    deps =
        [
            "//core/cc",
            "//core/memory/arena/cc",
            "//core/memory_tracker/cc",
            "//core/os/device/deviceinfo/cc",
            "//gapil/runtime/cc",
            "//gapis/api:api_cc_proto",
            "//gapis/api/vulkan/vulkan_pb:extras_cc_proto",
            "//gapis/capture:capture_cc_proto",
            "//gapis/memory/memory_pb:memory_pb_cc_proto",
            "@com_google_protobuf//:protobuf",
            "@spirv_reflect//:libspirv_reflect",
        ] + select({
            "//tools/build:fuchsia-arm64": [
                "@fuchsia_sdk//fidl/fuchsia.gpu.agis:fuchsia.gpu.agis_llcpp_cc",
                "@fuchsia_sdk//pkg/fdio",
            ],
            "//tools/build:fuchsia-x86_64": [
                "@fuchsia_sdk//fidl/fuchsia.gpu.agis:fuchsia.gpu.agis_llcpp_cc",
                "@fuchsia_sdk//pkg/fdio",
            ],
            "//conditions:default": [],
        }),
    alwayslink = True,
)

cc_dynamic_library(
    name = "libgapii",
    visibility = ["//visibility:public"],
    exports = "gapii_desktop.exports",
    deps = [":cc"],
)

android_dynamic_library(
    name = "libgapii_android",
    visibility = ["//visibility:public"],
    exports = "gapii_android.exports",
    deps = [":cc"],
)
